package hotel.compare.oversea.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import hotel.base.oversea.constant.CommonConstants;
import hotel.base.oversea.constant.CommonUrlConstants;
import hotel.base.oversea.constant.enums.CompareRequestUrlEnums;
import hotel.base.oversea.dto.master.CompareHotelBasicRoomDto;
import hotel.base.oversea.dto.meituan.MtRoomInfoDto;
import hotel.base.oversea.entity.master.CompareHotelBasicRoom;
import hotel.base.oversea.service.impl.BaseServiceImpl;
import hotel.base.oversea.utils.JacksonUtil;
import hotel.base.oversea.utils.Result;
import hotel.base.oversea.utils.StringUtil;
import hotel.compare.oversea.config.service.RedisService;
import hotel.compare.oversea.entity.master.CompareHotelRoom;
import hotel.compare.oversea.mapper.master.CompareHotelBasicRoomMapper;
import hotel.compare.oversea.mapper.master.CompareHotelRoomMapper;
import hotel.compare.oversea.mapper.switchs.SwitchCreateBasicRoomMapper;
import hotel.compare.oversea.service.CompareHotelBasicRoomService;
import hotel.compare.oversea.service.CompareRestTempaleUtilsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author lmf
 * @Date 2020/5/29 13:57
 * 效验房型信息
 **/
@Slf4j
@Service
public class CompareHotelBasicRoomServiceImpl extends BaseServiceImpl<CompareHotelBasicRoom> implements CompareHotelBasicRoomService {
    @Autowired
    CompareRestTempaleUtilsService compareRestTempaleUtilsService;
    @Autowired
    RedisService redisService;
    /**
     * 有效的效验表
     */
    @Autowired
    private CompareHotelBasicRoomMapper compareHotelBasicRoomMapper;
    /**
     * 失效的映射关系表
     */
    @Autowired
    private CompareHotelRoomMapper compareHotelRoomMapper;

    @Autowired
    private SwitchCreateBasicRoomMapper switchCreateBasicRoomMapper;

    @Override
    public Result<Object> getHotelBasicRoomInfo() {
        CompareHotelBasicRoomDto hotelBasicRoomInfo = compareHotelBasicRoomMapper.getHotelBasicRoomInfo();
        Result<Object> result = new Result<>();
        if(null != hotelBasicRoomInfo) {
            //先更新数据库字段 为已创建，创建失败会再改回去
            hotelBasicRoomInfo.setCreateState(1);
            compareHotelBasicRoomMapper.updateCreateStateById(hotelBasicRoomInfo);
            result.setData(hotelBasicRoomInfo);
        }
        return result;
    }

    @Override
    public Result<Object> updateCreateStateById(CompareHotelBasicRoomDto hotelBasicRoomInfo) {
        compareHotelBasicRoomMapper.updateCreateStateById(hotelBasicRoomInfo);
        return new Result<>();
    }
    /**
     * 根据携程母物理房型id 美团物理房型id 获取效验房型记录信息
     * @param switchRoomId
     * @param mtRoomId
     * @return
     */
    @Override
    public Result<CompareHotelBasicRoom> getCompareHotelBasicRoomInfo(int switchRoomId, int mtRoomId) {
        CompareHotelBasicRoom compareHotelBasicRoom=compareHotelBasicRoomMapper.getCompareHotelBasicRoomInfo(switchRoomId,mtRoomId);
        return new Result<>(compareHotelBasicRoom);
    }
    /**
     * @author: lmf
     * @date: 2020/9/19 16:29
     * 根据携程的母酒店id + 母物理房型id 获取到美团对应的酒店id+ 房型id
     */
    @Override
    public Result<CompareHotelBasicRoom> getHotelRoomInfo(int masterHotelId, int roomId) {
        CompareHotelBasicRoom compareHotelBasicRoom=compareHotelBasicRoomMapper.getHotelRoomInfo(masterHotelId,roomId);
        if(ObjectUtils.isEmpty(compareHotelBasicRoom)){
            //如果不存在效验数据
           /* SwitchCreateBasicRoomEntity switchCreateBasicRoomEntity=new SwitchCreateBasicRoomEntity();
            switchCreateBasicRoomEntity.setMasterHotelId(masterHotelId);
            switchCreateBasicRoomEntity.setMasterBasicRoomId(roomId);*/
            //switchCreateBasicRoomMapper.delete(switchCreateBasicRoomEntity);
            return new Result<>();
        }
        String key="compare_meituan_hotel_room_"+compareHotelBasicRoom.getMtHotelId();
        List<MtRoomInfoDto> list=new ArrayList<>();
        if(redisService.exists(key)){
           String json= (String) redisService.get(key);
           list= JacksonUtil.jsonToList(json,MtRoomInfoDto.class);
        }else{
            //判断美团的房型id是否发生变动
            String url = CommonUrlConstants.MEITUAN_HOST + CompareRequestUrlEnums.GET_MEI_TUAN_HOTEL_ROOM_LIST.getUrl();
            Map<String, Object> params=new HashMap<>();
            params.put("mtHotelId",compareHotelBasicRoom.getMtHotelId());
            try {
                Result<List<MtRoomInfoDto>> result = compareRestTempaleUtilsService.sendPostToMeiTuanHotelRoomList(url, params);
                if (result.getCode() == CommonConstants.SUCCESS) {
                    log.info("=====请求获取效验房型控制器成功,美团的房型id为:{}=======", result.getData());
                    list=result.getData();
                    if(!ObjectUtils.isEmpty(list)&&list.size()>0){
                        redisService.set(key, JSON.toJSONString(list),(long)43200);
                    }
                } else {
                    return new Result<>(compareHotelBasicRoom);
                }
            } catch (Exception e) {
                return new Result<>(compareHotelBasicRoom);
            }
        }
        if(!ObjectUtils.isEmpty(list)){
            for(MtRoomInfoDto mtRoomInfoDto:list){
                log.info("============房型名称为{}=======",mtRoomInfoDto.getRoomName());
                if(mtRoomInfoDto.getRoomName().equals(compareHotelBasicRoom.getMtRoomName())){
                    log.info("===========房型名称为{},效验表的房型名称为{},房型id为{}========",mtRoomInfoDto.getRoomName(),compareHotelBasicRoom.getMtRoomName(),mtRoomInfoDto.getRoomId());
                    if(Integer.parseInt(compareHotelBasicRoom.getMtHotelRoomId()) != mtRoomInfoDto.getRoomId()){
                        compareHotelBasicRoom.setMtRoomId(mtRoomInfoDto.getRoomId().toString());
                        Example example=new Example(CompareHotelBasicRoom.class);
                        Example.Criteria criteria=example.createCriteria();
                        criteria.andEqualTo("switchMasterHotelId",masterHotelId);
                        criteria.andEqualTo("switchHotelRoomId",roomId);
                        criteria.andEqualTo("mtHotelId",compareHotelBasicRoom.getMtHotelId());
                        criteria.andEqualTo("mtRoomName",mtRoomInfoDto.getRoomName());
                        CompareHotelBasicRoom compareHotelBasicRooms=new CompareHotelBasicRoom();
                        compareHotelBasicRooms.setMtHotelRoomId(mtRoomInfoDto.getRoomId().toString());
                        compareHotelBasicRoomMapper.updateByExampleSelective(compareHotelBasicRooms,example);
                    }
                }
            }
        }
        log.info("===============调用效验接口返回的效验数据为{}==========", JSON.toJSONString(compareHotelBasicRoom));
        return new Result<>(compareHotelBasicRoom);
    }

    /**
     * 根据携程母物理房型id+美团物理房型id 获取是否有效验数据
     * @param switchRoomId
     * @param mtRoomId
     * @return
     */
    @Override
    public Result<CompareHotelBasicRoom> getHotelRoomInfoBySwitchRoomIdAndMtRoomId(int switchRoomId, int mtRoomId) {
        CompareHotelBasicRoom compareHotelBasicRoom = compareHotelBasicRoomMapper.getHotelRoomInfoBySwitchRoomIdAndMtRoomId(switchRoomId, mtRoomId);
        return new Result<>(compareHotelBasicRoom);
    }

    /**
     * 根据美团酒店id+美团物理房型id 获取是否有效验数据
     * @param mtHotelId
     * @param mtRoomId
     * @return
     */
    @Override
    public Result<CompareHotelBasicRoom> getHotelRoomInfoByMtHotelIdAndMtRoomId(int mtHotelId, int mtRoomId) {
        CompareHotelBasicRoom compareHotelBasicRoom = compareHotelBasicRoomMapper.getHotelRoomInfoByMtHotelIdAndMtRoomId(mtHotelId, mtRoomId);
        return new Result<>(compareHotelBasicRoom);
    }

    /**
     * 根据美团酒店id+美团物理房型id 获取是否有效验数据
     * @param mtHotelId
     * @param mtRoomId
     * @return
     */
    @Override
    @Transactional
    public Result<Object> removeHotelRoomInfoByMtHotelIdAndMtRoomId(int mtHotelId, int mtRoomId) {
        CompareHotelBasicRoom compareHotelBasicRoom = compareHotelBasicRoomMapper.getHotelRoomInfoByMtHotelIdAndMtRoomId(mtHotelId, mtRoomId);
        if(ObjectUtil.isEmpty(compareHotelBasicRoom)){
            return new Result<>();
        }else{
            CompareHotelRoom compareHotelRoom=new CompareHotelRoom();
            BeanUtils.copyProperties(compareHotelBasicRoom, compareHotelRoom);
            compareHotelRoom.setId(compareHotelRoom.getId()+ StringUtil.getFourRandom());
            int flag=compareHotelRoomMapper.insert(compareHotelRoom);
            if(flag>0){
                int f=compareHotelBasicRoomMapper.removeHotelRoomInfoByMtHotelIdAndMtRoomId(mtHotelId, mtRoomId);
                if(f>0){
                    return new Result<>();
                }else{
                    return new Result<>("清除失败");
                }
            }
            return new Result<>();
        }

    }
}
